package com.mooc.ppjoke.ui.my_view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mooc.lib_common.PixUtils;
import com.mooc.ppjoke.R;
import com.mooc.ppjoke.data.BottomBar;
import com.mooc.ppjoke.data.Destination;
import com.mooc.ppjoke.utils.AppConfig;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @ClassName: AppBottomBar$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/14 18:25
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class AppBottomBar extends BottomNavigationView {
    private static int[] sIcons = new int[]{R.drawable.icon_tab_home, R.drawable.icon_tab_sofa, R.drawable.icon_tab_publish, R.drawable.icon_tab_find, R.drawable.icon_tab_mine};
    public AppBottomBar(Context context) {
        this(context,null);
    }

    public AppBottomBar(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    @SuppressLint("RestrictedApi")
    public AppBottomBar(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        BottomBar bottomBar = AppConfig.getBottomBar();
        List<BottomBar.TabsBean> tabs = bottomBar.getTabs();
        int[][] states=new int[2][];
        states[0]=new int[]{ android.R.attr.state_selected};
        states[1]=new int[]{};
        int[] colors=new int[]{Color.parseColor(bottomBar.getActiveColor()),Color.parseColor(bottomBar.getInActiveColor())};
        ColorStateList colorStateList=new ColorStateList(states, colors);
        setItemIconTintList(colorStateList);
        setItemTextColor(colorStateList);
        setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
//        setSelectedItemId(bottomBar.getSelectTab());
        for (int i = 0; i < tabs.size(); i++) {
            BottomBar.TabsBean tabsBean=tabs.get(i);
            if(!tabsBean.getEnable()){
                return;
            }
            int id = getId(tabsBean.getPageUrl());
            if(id<0){
                continue;
            }
            MenuItem item = getMenu().add(0, id, tabsBean.getIndex(), tabsBean.getTitle());
            item.setIcon(sIcons[tabsBean.getIndex()]);
        }
        for (int i = 0; i < tabs.size(); i++) {
            BottomBar.TabsBean tabsBean = tabs.get(i);
            int iconSize= PixUtils.dp2px(tabsBean.getSize());
            BottomNavigationMenuView menuView = (BottomNavigationMenuView) getChildAt(0);
            BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(i);
            itemView.setIconSize(iconSize);
            if(TextUtils.isEmpty(tabsBean.getTitle())){
                int tintColor = TextUtils.isEmpty(tabsBean.getTintColor()) ? Color.parseColor("#ff678f") : Color.parseColor(tabsBean.getTintColor());
                itemView.setIconTintList(ColorStateList.valueOf(tintColor));
                itemView.setShifting(false);
            }
        }
    }


    private int getId(String pageUrl) {
        Destination destination = AppConfig.getsDestConfig().get(pageUrl);
        if(destination==null){
            return  -1;
        }
        return destination.getId();
    }
}
